import { reactive, ref, onMounted, toRaw } from "vue";
import { getSystemLog } from "@/api/systemcofig";
import { type PaginationProps } from "@pureadmin/table";
export function useSystemlog() {
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true,
    searchText: ""
  });
  // const form = reactive({
  //   onePage: pagination
  // });
  const columns: TableColumnList = [
    {
      label: "编号",
      prop: "id"
    },
    {
      label: "日期",
      prop: "date"
    },
    {
      label: "日志类型",
      prop: "thread"
    },
    {
      label: "操作人员",
      prop: "logger"
    },
    {
      label: "功能",
      prop: "message"
    },
    {
      label: "机构代码",
      prop: "comID"
    },
    {
      label: "代码",
      prop: "exception"
    }
  ];

  async function onSearch() {
    loading.value = true;
    const Result = await getSystemLog(toRaw(pagination));
    dataList.value = Result.result;
    pagination.total = Result.onePage.total;
    pagination.pageSize = Result.onePage.pageSize;
    pagination.currentPage = Result.onePage.currentPage;

    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };
  function handleSizeChange(val: number) {
    pagination.pageSize = val;
    onSearch();
  }

  function handleCurrentChange(val: number) {
    pagination.currentPage = val;
    onSearch();
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }

  onMounted(() => {
    onSearch();
  });

  return {
    // form,
    loading,
    columns,
    dataList,
    pagination,
    resetForm,
    onSearch,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
